package com.gxuwz.attendance.dao;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import com.gxuwz.attendance.entity.Student;
import com.gxuwz.attendance.entity.User;
import com.gxuwz.attendance.vo.Page;
import net.sf.json.JSONArray;

@Repository("StudentDao")
public class StudentDao extends SessionFactoryDao{
	
	/**
	 * 添加学生信息
	 * @param student
	 * @return
	 */
	public boolean add(Student student) {
		boolean flag = true;
		
		Session session = null;
		
		long timeMillis = System.currentTimeMillis();
		
		String time = timeMillis + "";
		
		try {
			session = getSession();
			student.setStudentNo("2019" + time.substring(8));
			
			// 开启事务
			session.beginTransaction();
			session.save(student);
			session.getTransaction().commit();
		}catch (Exception e) {
			// 出现异常更新失败
			flag = false;
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		
		return flag;
	}
	
	/**
	 * 更新学生信息
	 * @param student
	 * @return
	 */
	public boolean update(Student student) {
		boolean flag = true;
		Session session = null;
		try {
			session = getSession();
			// 开启事务
			session.beginTransaction();
			session.update(student);
			session.getTransaction().commit();
		}catch (Exception e) {
			// 出现异常更新失败
			flag = false;
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		
		return flag;
	}
	
	
	/**
	 * 批量删除学生信息
	 * @param ids
	 * @return
	 */
	public boolean delete(String ids) {
		JSONArray array = JSONArray.fromObject(ids);

		Session session = null;
		session = getSession();
		
		boolean flag = true;
		
		session.beginTransaction();
		try {
			
			for(int i = 0; i < array.size(); i++) {
				Student student = new Student();
				
				student.setStudentNo(array.getJSONObject(i).getString("studentNo"));
				
				session.delete(student);
			}
			session.getTransaction().commit();
			
		}catch(Exception e) {
			flag = false;
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally {
			session.close();
		}
		
		return flag;
	}
	
	
	/**
	 * 根据主键查找学生
	 * @param studentNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Student findByOne(String studentNo) {
		String hql = "from Student where studentNo = '" + studentNo + "'";
		
		List<Student> list = getSession().createQuery(hql).list();
		Student s = null;
		if(!list.isEmpty()) {
			s = (Student) getSession().createQuery(hql).list().get(0);
		}
		
		return s;
	}
	
	
	
	/**
	 * 分页查询学生信息
	 * @param studentName
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Student> findByPage(Student student, Page page){
		List<Student> list = new ArrayList<Student>();
		
		// 获取当前用户类型
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String utype = (String) session.getAttribute("utype");
		User currentUser  = (User) session.getAttribute("user");
		
		
		String hql = "";
		if(student.getStudentName() == null || "".equals(student.getStudentName())) {
			hql = "From Student where 1=1";
			// 当前用户类型是老师
			if(utype.equals("3")) {
				hql = "From Student where 1=1";
			}
		}else {
			hql = "From Student where studentName like '%" + student.getStudentName() + "%'";
			// 当前用户类型是老师
			if(utype.equals("3")) {
				
			}
		}
		if(student.getClassNo() != null && !"".equals(student.getClassNo())) {
			hql = hql + "and classNo = '" + student.getClassNo() + "'"; 
		}
		
		list = getSession().createQuery(hql).list();
		// 保存查询总数
		session.setAttribute("studentSize", list.size());
		
		if(!list.isEmpty()) {
			if(page.getRows()*page.getPage() > list.size()) {
				list = list.subList(page.getOffset(), list.size());
			}else {
				list = list.subList(page.getOffset(), page.getRows()*page.getPage());
				
			}
		}
		
		return list;
	}
	
	/**
	 * 模糊查询记录总数
	 * @param studentName
	 * @return
	 */
	public Integer getTotal(String studentName) {
		
		String sql = "";
		if(studentName == null || "".equals(studentName)) {
			sql = "select * from sys_student ";
		}else {
			sql = "select * from sys_student where student_name like '%" + studentName + "%'";
		}
		List list = getSession().createSQLQuery(sql).list();
		
		return list.size();
	}
	
	
}
